﻿#region Copyright
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx
#endregion

#region Imports

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Data;
using DesignByContract;
using Majesty_of_Omega.Config;
using Majesty_of_Omega.GUI.Controller;
using Majesty_of_Omega.GUI.Pages.Game.Research.Parts;
using Majesty_of_Omega.Model.Research;

#endregion

namespace Majesty_of_Omega.GUI.ValueConverters
{

    ///<summary>
    /// The TechLevelConverter retrieves all Technlogy Level Infos for a given Technology field. 
    /// The first visible tech level will be given
    ///</summary>
    public class TechLevelConverter : IMultiValueConverter
    {
        /// <summary>
        /// Gets the maximal possible tech level.
        /// </summary>
        /// <remarks>
        /// This value will be databound in the <see cref="TechnologyMatrixPart.TechLevelScrollbar"/>
        /// </remarks>
        /// <value>The max tech level.</value>
        public int MaxTechLevel
        {
            get
            {
                return SpreadSheetManager.TechTables.MaxTechLevel;
            }
        }

        #region IMultiValueConverter Members

        ///<summary>
        /// Retrieves a number of technology level infos <see cref="TechLevelInfo"/> 
        /// from the given startig technology level (Parameter 1) til StartLevel + MaxTechLevel
        /// from the given field of technology <see cref="TechFieldResearch"/>
        /// </summary>
        ///<param name="values">
        /// Two values: 
        /// <list type="bullet">
        /// <item>The researched technology field </item>
        /// <item>The first technology level, which should be drawn</item>
        /// </list>
        /// </param>
        ///<param name="targetType"></param>
        ///<param name="parameter"></param>
        ///<param name="culture"></param>
        ///<returns></returns>
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Require.AreEqual(2, values.Length, "There must be two parameters");
            Require.IsTrue(values[0] is TechFieldResearch, String.Format("Unprocessable value for Convert: {0}", values[0]));
            Require.IsTrue(values[1] is double);

            int startTechLevel = System.Convert.ToInt32(values[1]);
            int endTechLevel = Math.Min(startTechLevel + UIConstants.MaximumVisibleTechLevels, MaxTechLevel);

            TechFieldResearch fieldResearch = (TechFieldResearch)values[0];
            List<TechLevelInfo> levelInfos = new List<TechLevelInfo>();
            for (int i = startTechLevel; i < endTechLevel; i++)
            {
                levelInfos.Add(new TechLevelInfo(i, fieldResearch));
            }
            return levelInfos;
        }

        ///<summary>
        /// This function is not supported
        ///</summary>
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
